package com.itheima.question.timer;

import com.itheima.common.contants.RedisPicConstants;
import com.itheima.common.safe.PicSafe;
import com.itheima.common.safe.TextSafe;
import com.itheima.question.config.FastDFSClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Date;
import java.util.Set;

/**
 * @ClassName TaskDemo
 * @Description TODO
 * @Author Mister-Lu
 * @Date 2021/12/22
 **/
//@Component
@Slf4j
public class ClearPicTask {
    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Scheduled(cron = "0/5 * * * * ?")
    public void clearPic(){
        Set<String> pics = redisTemplate.opsForSet().difference(RedisPicConstants.ALL_PIC, RedisPicConstants.USE_PIC);
        log.info("定时任务开始执行："+pics);
        assert pics != null;
        for(String pic : pics){
            fastDFSClientUtil.delFile(pic);
            PicSafe.picSafe(fileServerUrl+pic);
        }


        System.out.println("hello spring task"+ new Date());
    }
}